<template>
  <div class="app-container">
    <el-row :gutter="20">

      <el-col  >
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

          <el-form-item label="服务方" prop="supplierId">
            <el-select v-model="queryParams.supplierId" clearable placeholder="请选择服务方" @change="changeSupplier">
              <el-option v-for="item in supplierList"
                :key="item.supplierId"
                :value="item.supplierId"
                :label="item.supplierName"
              ></el-option>
            </el-select>
          </el-form-item>

        <el-form-item label="接口名称" prop="portId">
          <el-select v-model="queryParams.portId" clearable placeholder="请选择接口">
            <el-option v-for="item in portList"
                :key="item.id"
                :value="item.id"
                :label="item.portName"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="请求时间" prop="toTime">
          <el-date-picker clearable size="small"
            v-model="queryParams.toTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择请求时间">
          </el-date-picker>
        </el-form-item>
          <el-form-item label="报错标识" prop="errorFlag">
            <el-select
              v-model="queryParams.errorFlag"
              placeholder="报错标识"
              clearable
              style="width: 240px">
              <el-option
                v-for="dict in dict.type.pm_log_error_flag"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--        <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['pm:excLog:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['pm:excLog:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['pm:excLog:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['pm:excLog:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

        <el-table v-loading="loading" :data="excLogList" @selection-change="handleSelectionChange">

          <el-table-column label="服务方" align="center" prop="supplierName" />
          <el-table-column label="接口名称" align="center" prop="portName" />


          <el-table-column label="请求时间" align="center" prop="toTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.toTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>

          <el-table-column label="返回时间" align="center" prop="outTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.outTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>

          <el-table-column label="请求报文" align="center"  width="120px" show-overflow-tooltip >
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-copy-document" size="mini" v-copy="scope.row.toData" >复制</el-button>
              {{ scope.row.toData }}
            </template>
          </el-table-column>

          <el-table-column label="返回报文" align="center"  width="120px" show-overflow-tooltip >
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-copy-document" size="mini"  v-copy="scope.row.outData" >复制</el-button>
              {{ scope.row.outData }}
            </template>
          </el-table-column>
          <el-table-column label="报错信息" align="center"  width="120px" show-overflow-tooltip >
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-copy-document" size="mini"  v-copy="scope.row.exceptionData" >复制</el-button>
              {{ scope.row.exceptionData }}
            </template>
          </el-table-column>

          <el-table-column prop="errorFlag" label="错误标识" width="80">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.pm_log_error_flag" :value="scope.row.errorFlag"/>
            </template>
          </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['pm:excLog:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['pm:excLog:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
       </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>

    </el-row>
  </div>
</template>

<script>
import { listExcLog, getExcLog, delExcLog, addExcLog, updateExcLog } from "@/api/pm/excLog";
import { listExcPort } from "@/api/pm/excPort";

import ChannelHeadTree from '@/components/HeadTree/channel';
import Supplier from '@/mixin/supplier';

import {copyData} from '@/utils/common'

export default {
  name: "ExcLog",
  dicts: ['pm_log_error_flag'],
  components: {ChannelHeadTree},
  mixins: [Supplier],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 接口监控日志表格数据
      excLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierId: null,
        portId: null,
        portName: null,
        orderJson: null,
        updateJson: null,
        toTime: null,
        outTime: null,
        toData: null,
        outData: null,
        exceptionData: null,
        errorFlag: "0",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        channelCode: [
          { required: true, message: "渠道编码不能为空", trigger: "blur" }
        ],
        orderId: [
          { required: true, message: "订单id不能为空", trigger: "blur" }
        ],
      },
      portList: []
    };
  },
  created() {
    //this.getList();
  },
  methods: {
    /** 查询接口监控日志列表 */
    getList() {
      this.loading = true;
      listExcLog(this.queryParams).then(response => {
        this.excLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        portId: null,
        portName: null,
        toTime: null,
        outTime: null,
        toData: null,
        outData: null,
        exceptionData: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加接口监控日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getExcLog(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改接口监控日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateExcLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExcLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除接口监控日志编号为"' + ids + '"的数据项？').then(function() {
        return delExcLog(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pm/excLog/export', {
        ...this.queryParams
      }, `excLog_${new Date().getTime()}.xlsx`)
    },

    // 执行复制操作
    handlerCopy(data) {
      copyData(data).then(()=>{
        this.$modal.msgSuccess('复制成功！');
      })
    },

    chooseTree(data) {
      this.queryParams.channelCode = data?.channelCode || '';
      if(this.queryParams.channelCode) {
        this.getList();
      } else {
        this.excLogList = [];
        this.total = 0;
      }
    },

    // 服务方改变
    async changeSupplier() {
      const params = {
        supplierId: this.queryParams.supplierId
      };
      const res = await listExcPort(params);
      this.portList = res?.rows ?? [];
    }
  }
};
</script>
